#include "flash_driver.h"

#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

#include <cassert>


using namespace flash;

#ifdef _PLATFORM_WII



#else // !_PLATFORM_WII
#include "gameswf/gameswf.h"
#include "gameswf/gameswf_player.h"

#include "graphics/graphics.h"

#include "gameswf/base/tu_file.h"
#include "gameswf/base/utility.h"
#include "gameswf/base/container.h"
#include "gameswf/base/tu_file.h"
#include "gameswf/base/tu_types.h"

FlashDriver::FlashDriver() : 
	// seperate class later
	m_pSound(NULL),
	m_pRender(NULL),
	m_pPlayer(NULL)
{

}

FlashDriver::~FlashDriver()
{
	UTIL_SAFE_DELETE(m_pSound);
	UTIL_SAFE_DELETE(m_pRender);
	gameswf::set_render_handler(NULL);
	gameswf::set_sound_handler(NULL);
}

void	FlashDriver::Init()
{
	gameswf::register_file_opener_callback(file_opener);
	gameswf::register_fscommand_callback(fs_callback);
	gameswf::register_log_callback(log_callback);

//	sound = gameswf::create_sound_handler_sdl();
//	gameswf::set_sound_handler(sound);

	m_pRender = (gameswf::render_handler*)GetGraphics()->GetFlashDriver();
	m_pSound = gameswf::create_sound_handler_bass();
	gameswf::set_render_handler(m_pRender);
	gameswf::set_sound_handler(m_pSound);

	m_pRender->open();
	m_pRender->set_antialiased(true);

	m_pPlayer = new gameswf::player();
}


gameswf::gc_ptr<gameswf::root>	FlashDriver::Load(const std::string& File)
{
	if (!m_pPlayer) return NULL;
	return m_pPlayer->load_file(File.c_str());
}


void		FlashDriver::log_callback(bool error, const char* message)
{
	if (!message) return;
	if (error)
		_LOG(MSG_ERROR, message);
	else
		_LOG(MSG_COMMENT, message);
}
tu_file*	FlashDriver::file_opener(const char* url)
{
	// replace this with a call to VFS?
	return new tu_file(url, "rb");

}
void		FlashDriver::fs_callback(gameswf::character* movie, const char* command, const char* arg)
{
	_LOG(MSG_COMMENT, "fs_callback: '");
	_LOG(MSG_COMMENT, "   Command  : " << command);
	_LOG(MSG_COMMENT, "   Argument : " << arg);
}

#endif //